from rest_framework import serializers
from .models import CourseCategory, Course, Teacher, CourseChapter, CourseSection


class CourseCategoryModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseCategory
        fields = ['id', 'name']


class TeacherModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Teacher
        fields = ['id', 'name', 'role_name', 'title', 'signature', 'image', 'brief']


class CourseModelSerializer(serializers.ModelSerializer):
    teacher = TeacherModelSerializer()

    # course_section=serializers.SerializerMethodField()
    # def get_course_section(self):
    #     pass
    class Meta:
        model = Course
        fields = ['id', 'name', 'course_img',
                  'course_type_name',
                  'brief',
                  'level_name',
                  'period',
                  'attachment_path',
                  'status_name', 'students', 'sections',
                  'pub_sections', 'price',
                  'teacher', 'course_category', 'course_section'

                  ]


class CourseSectionModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseSection
        fields = ['name', 'orders', 'section_type', 'section_link', 'duration', 'free_trail']


class CourseChapterModelSerializer(serializers.ModelSerializer):
    # 子序列化方式实现,一个章节下有很多课时，必须写many=True
    coursesections = CourseSectionModelSerializer(many=True)

    class Meta:
        model = CourseChapter
        fields = ['course', 'chapter', 'name', 'summary', 'coursesections']
